package com.session05.pm;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Calculator extends JFrame {

    private JTextField txtInput;
    private JPanel mainPanel;

    private String op;

    private double num1, num2;
    private String result;

    private boolean flag;

    public Calculator() {
        this.setTitle("计算器");
        this.setBounds(400, 100, 300, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        txtInput = new JTextField();
        //字体框大小
        txtInput.setPreferredSize(new Dimension(280, 50));
        //字体框字体
        txtInput.setFont(new Font("TimesRoman",Font.ITALIC,25));
        //字体框字体颜色
        txtInput.setDisabledTextColor(Color.BLACK);
        //设置是否可用
        txtInput.setEnabled(false);
        //设置对齐方式
        txtInput.setHorizontalAlignment(JTextField.RIGHT);
        this.add(txtInput, BorderLayout.NORTH);

        mainPanel = new JPanel();
        //设置panel容器为网格布局
        mainPanel.setLayout(new GridLayout(4, 4, 5, 5));
        mainPanel.add(getButton("7"));
        mainPanel.add(getButton("8"));
        mainPanel.add(getButton("9"));
        mainPanel.add(getButton("*"));
        mainPanel.add(getButton("4"));
        mainPanel.add(getButton("5"));
        mainPanel.add(getButton("6"));
        mainPanel.add(getButton("/"));
        mainPanel.add(getButton("1"));
        mainPanel.add(getButton("2"));
        mainPanel.add(getButton("3"));
        mainPanel.add(getButton("+"));
        mainPanel.add(getButton("0"));
        mainPanel.add(getButton("."));
        mainPanel.add(getButton("="));
        mainPanel.add(getButton("-"));
        this.add(mainPanel);
    }

    /**
     * 统一生成按钮的方法
     *
     * @param key 按钮上的文字
     * @return JButton按钮
     */
    private JButton getButton(String key) {
        JButton btn = new JButton(key);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if (flag) {
                    txtInput.setText("");
                    num1 = 0;
                    num2 = 0;
                    result = "0";
                    op = "";
                    flag = false;
                }
                //返回时间本身getSource()
                //JButton button = (JButton)e.getSource();
                //获取按钮上的文字
                String str = e.getActionCommand();

                switch (str) {
                    case "0":
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":
                    case "9":
                        if (txtInput.getText().equals("") && str.equals("0")) {
                            txtInput.setText("0");
                        } else if (txtInput.getText().equals("0")) {
                            return;
                        } else {
                            txtInput.setText(txtInput.getText() + str);
                        }
                        break;
                    case ".":
                        //如果输入框中已经有了.
                        //indexOf String字符串的方法，判断字符串中是否包含有某个字符
                        //如果有返回这个字符在该字符串中的下标位置
                        //如果没有，返回-1
                        if (txtInput.getText().indexOf(".") != -1) {
                            return;
                        } else if (txtInput.getText().equals("")) {
                            txtInput.setText("0.");
                            return;
                        }
                        txtInput.setText(txtInput.getText() + str);
                        break;
                    case "+":
                        //包装类Double，把字符串转换为double类型
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "+";
                        txtInput.setText("");
                        break;
                    case "-":
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "-";
                        txtInput.setText("");
                        break;
                    case "*":
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "*";
                        txtInput.setText("");
                        break;
                    case "/":
                        num1 = Double.parseDouble(txtInput.getText());
                        op = "/";
                        txtInput.setText("");
                        break;
                    case "=":
                        num2 = Double.parseDouble(txtInput.getText());
                        switch (op) {
                            case "+":
                                result = num1 + num2 + "";
                                break;
                            case "-":
                                result = num1 - num2 + "";
                                break;
                            case "/":
                                if (num2 == 0) {
                                    result = "错误";
                                } else {
                                    result = num1 / num2 + "";
                                }
                                break;
                            case "*":
                                result = String.valueOf(num1 * num2);
                                break;
                        }
                        txtInput.setText(result);
                        flag = true;
                        break;
                }
            }
        });
        return btn;
    }

    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        calculator.setVisible(true);
    }
}
